﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Power.DAL;
using Power.Model;

namespace Power
{
    /// <summary>
    /// Signup.xaml 的交互逻辑
    /// </summary>
    public partial class Signup : Window
    {
        public Signup()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            bool isOK = true;



            string usna = Usernamebox.Text;
            string password1 = passwordBox1.Password;
            string password2 = passwordBox2.Password;
            string Emid = Emidbox.Text;

            CheckTextboxNotEmpty(ref isOK, Usernamebox, Emidbox);
            CheckPassboxNotEmpty(ref isOK, passwordBox1, passwordBox2);
            if (!isOK)
            {
                MessageBox.Show("有未填写信息");
                return;
            }

            
            bool rename =new UserDal().JudgeByUserName(usna);
            
            if (password1 != password2)
            {
                MessageBox.Show("两次输入密码不同");

            }
            else if (rename)
            {
                MessageBox.Show("存在重名");
            }
            else
            {
                User op=new User();
                op.EmployeeID = Emid;
                op.UserName = usna;
                op.Password = CommonHelper.GetMD5(password1 + CommonHelper.GetPasswordSalt());
                op.Usertype = 0;
                op.IsDeleted = false;
                new UserDal().Insertnew(op);
                MessageBox.Show("注册成功");
                DialogResult = false;
 
            }


        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }


        private void CheckTextboxNotEmpty(ref bool isOK,
    params TextBox[] textboxes)
        {
            foreach (TextBox txtBox in textboxes)
            {
                if (txtBox.Text.Length <= 0)
                {
                    isOK = false;
                    txtBox.Background = Brushes.Red;
                }
                else
                {
                    txtBox.Background = null;
                }
            }
        }

        private void CheckPassboxNotEmpty(ref bool isOK,
params PasswordBox[] passes)
        {
            foreach (PasswordBox passBox in passes)
            {
                if (passBox.Password.Length <= 0)
                {
                    isOK = false;
                    passBox.Background = Brushes.Red;
                }
                else
                {
                    passBox.Background = null;
                }
            }
        }



    }
}
